package InterClass03;

interface InterfaceA {
    int method(int n);
}
//先复习完Test再来看这个
//这样多一个函数来实现其实不如在匿名类重新时候把输出写出来，把这个函数功能完成

public class niMingLeiDuoTai {

    public static void compute(InterfaceA a) {
        int res;
        res = a.method(5);
        System.out.println(res);
    }

    public static void main(String[] args) {

        //通过匿名内部类实现接口InterfaceA计算前5个整数之和
        InterfaceA aa = new InterfaceA() {
            public int method(int n) {
                int sum = 0;
                for (int i = 1; i <= n; i++) {
                    sum += i;
                }
                return sum;
            }
        };
        compute(aa);


        //通过匿名内部类实现接口InterfaceA计算前5个整数的阶乘
        InterfaceA bb = new InterfaceA() {
            public int method(int n) {
                int sum = 1;
                for (int i = 1; i <= n; i++) {
                    sum *= i;
                }
                return sum;
            }
        };
        compute(bb);
    }


}






